import Vue from 'vue'
import Toast from './index.vue'

const ToastConstructor = Vue.extend(Toast);

// CreatToast 既作为函数也作为对象
let CreatToast;

CreatToast = (options) => {
    CreatToast.insert(options)
};

CreatToast.options = {};

// 创建 CreatToast 实例，并挂载到一个元素上。
CreatToast.insert = (options) => {
    if (typeof options === 'string') {
        options = {
            message: options
        }
    }
    const newInstance = new ToastConstructor({});
    Object.assign(newInstance, options);
    document.body.append(newInstance.$mount().$el)
    newInstance.showToast()
}
// 全局方法挂载
CreatToast.install = (Vue) => {
    Vue.prototype.$toast = CreatToast
}
// success
CreatToast.success = function (text) {
    CreatToast.options.message = text;
    CreatToast.options.toastType = 'success';
    CreatToast.insert(this.options)
}
// fail
CreatToast.fail = function (text) {
    CreatToast.options.message = text;
    CreatToast.options.toastType = 'fail';
    CreatToast.insert(this.options)
}

// 为什么不用class!! new不出来 

export default CreatToast